<?php
namespace app\admin\command;

use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\Db;
use think\WeChat\Pay;

class Transfers extends Command
{
    protected function configure()
    {
        $this->setName('transfers')->setDescription('提现');
    }

    protected function execute(Input $input, Output $output)
    {
        // $reward_switch = Db::name('config')->where(['name'=>'reward_switch','group'=>'switch'])->value('value');
        // if($reward_switch ==0){
        //    $output->writeln('奖励发放已关闭');
        //    die; 
        // }
        //订单类型:1=币币交易,2=OTC通道买入,3=OTC通道卖出
        //订单状态:0=已撤销,1=已完成,2=售卖中,3=待审核,4=待支付
		$all_order = Db::name('user_order')
        ->where(['order_type'=>3,'order_status'=>4])
        ->select();
        if($all_order){
            $reward_count = 0;
            $miner_count = 0;
            $config = wechat_config();
            // 启动事务
            // Db::startTrans();
            // try{
                foreach ($all_order as $key => $value) 
                {
                    $status = Db::name("user")->where('id', $value['user_id'])->value('status');
                    if($status!='normal'){
                        continue;
                    }
                    
                    $find = Db::name('third_party_user')
                    ->where(['app_id'=>$config['appid'],'user_id'=>$value['user_id']])
                    ->find();
                    if(!$find){
                        continue;
                    }
                    try {
                        $price = ($value['sell_price']-$value['transfer_fee'])*100;
                        $amount = round($price, 2);
                        //微信转账到零钱
                        $wechat = new \WeChat\Pay($config);

                        //组装参数，可以参考官方商户文档
                        $options = [
                            'partner_trade_no' => $value['order_no'],
                            'openid'           => $find['openid'],
                            'check_name'       => 'NO_CHECK',
                            'amount'           => $amount,
                            'desc'             => '提现',
                            'spbill_create_ip' => request()->ip(),
                        ];
                        $result = $wechat->createTransfers($options);
                        if($result['return_code']=='SUCCESS'){
                            if($result['result_code']=='SUCCESS'){

                            }
                        }
                        // echo '<pre>';
                        // var_export($result);
                        // $result = $wechat->queryTransfers($options['partner_trade_no']);
                        // var_export($result);

                    } catch (Exception $e) {

                        // 出错啦，处理下吧
                        //echo $e->getMessage() . PHP_EOL;

                    }
                }
            //     // 提交事务
            //     Db::commit(); 
            //     $output->writeln('矿机总数：'.$miner_count.',奖励次数：'.$reward_count);
            // }catch (\think\Exception\DbException $exception){
            //     Db::rollback();
            //     $output->writeln('错误记录：'.$exception->getMessage());
            // }
        }
        
    }
}